import type { MockMethod } from 'vite-plugin-mock'
import Mock from 'mockjs'
import type { IUserResponse } from '@/types/apis/user'
import dotenv from 'dotenv'

// node环境中获取环境变量
dotenv.config({ path: '.env.development' })
const prefix = process.env.VITE_APP_BASE_API

// 请求参数类型
type Recordable<T = any> = Record<string, T>
interface MockRequest {
  url: Recordable
  body: Recordable
  query: Recordable
  headers: Recordable
}

export default [
  // 1.获取用户列表
  {
    method: 'get',
    url: `${prefix}/mock/user`,
    response({ query }: MockRequest) {
      // const { pageSize } = query
      const data: IUserResponse[] = Array.from({ length: 40 }, () => {
        const user: IUserResponse = {
          id: Mock.mock('@id'),
          username: Mock.mock('@cname'),
          age: Mock.mock('@integer(20, 40)'),
          email: Mock.mock('@email'),
          phone: Mock.mock('@integer(10000000000, 19999999999)'),
          avatar: Mock.mock('@image(200x100, @color, @name)'),
          role: Mock.Random.pick(['admin', 'user']), // admin或user
          hobbies: Array.from({ length: Mock.Random.integer(1, 5) }, () => Mock.Random.word(2, 5)),
        }
        return user
      })
      return {
        code: 0,
        message: `获取用户列表成功`,
        data: data,
      }
    },
  },
  // 2.获取指定用户信息
  {
    url: `${prefix}/mock/user/:id`,
    method: 'get',
    response({ query }: MockRequest) {
      const { id } = query
      const data: IUserResponse = {
        id: id,
        username: Mock.mock('@cname'),
        age: Mock.mock('@integer(20, 40)'),
        email: Mock.mock('@email'),
        phone: Mock.mock('@integer(10000000000, 19999999999)'),
        avatar: Mock.mock('@image(200x100,rgb(75, 92, 74), Hello)'),
        role: 'admin', // admin或user
        hobbies: () => {
          const count = Mock.Random.integer(1, 5)
          return Array.from({ length: count }, () => Mock.Random.word(2, 5))
        },
      }
      return {
        code: 0,
        message: `获取id=${id}用户信息成功`,
        data: data,
      }
    },
  },
] satisfies MockMethod[]
